<?php
/**
 * Created by PhpStorm.
 * User: zhujingtao
 * Date: 2017/12/1
 * Time: 13:41
 */

use Illuminate\Support\Str;

/**
 * 加载RPCImplClass
 *
 * @param string $rpcname
 *
 * @return object
 */
function loadRpcImpl($rpcname)
{
    $path = RPC_DIR . '/' . strtolower($rpcname) . '.php';
    if (!file_exists($path)) {
        $path = RPC_DIR. '/' . strtoupper($rpcname) . '.php';
    }
    require_once $path;
    $rpcClass = "app\\rpc\\" . $rpcname;
    return new $rpcClass();
}

function loadRpcRoute(){
    $path = RPC_ROUTE . 'route.php';
    require_once $path;
}

/**
 *日志记录写文件
 */
function logs($arg, $logName = 'debug')
{

    $logName = $logName ? $logName : 'wanglibao';//日志名称
    $fp = fopen(LOG_DIR . '/' . $logName . '.log.' . date('Ymd'), 'a');

    $traces = debug_backtrace();
    $logMsg = 'FILE:' . basename($traces[0]['file']) . PHP_EOL;
    $logMsg .= 'FUNC:' . $traces[1]['function'] . PHP_EOL;
    $logMsg .= 'LINE:' . $traces[0]['line'] . PHP_EOL;

    if (is_string($arg)) {
        $logMsg .= 'ARGS:' . $arg . PHP_EOL;
    } else {
        $logMsg .= 'ARGS:' . var_export($arg, true) . PHP_EOL;
    }
    $logMsg .= 'DATETIME:' . date('Y-m-d H:i:s') . PHP_EOL . PHP_EOL;

    fwrite($fp, $logMsg);
    fclose($fp);
}

if (! function_exists('env')) {
    /**
     * Gets the value of an environment variable. Supports boolean, empty and null.
     *
     * @param  string  $key
     * @param  mixed   $default
     * @return mixed
     */
    function env($key, $default = null)
    {
        $value = getenv($key);

        if ($value === false) {
            return value($default);
        }

        switch (strtolower($value)) {
            case 'true':
            case '(true)':
                return true;
            case 'false':
            case '(false)':
                return false;
            case 'empty':
            case '(empty)':
                return '';
            case 'null':
            case '(null)':
                return;
        }

        if (strlen($value) > 1 && Str::startsWith($value, '"') && Str::endsWith($value, '"')) {
            return substr($value, 1, -1);
        }

        return $value;
    }
}

